//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class ManufacturerDetailsControl : BaseNopAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                Manufacturer manufacturer = this.ManufacturerService.GetManufacturerById(this.ManufacturerId);
                if (manufacturer != null) {
                    lblTitle.Text = Server.HtmlEncode(manufacturer.Name);
                }
            }
        }

        protected Manufacturer Save() {
            Manufacturer manufacturer = ctrlManufacturerInfo.SaveInfo();
            ctrlManufacturerSEO.SaveInfo();
            ctrlManufacturerProducts.SaveInfo();

            this.CustomerActivityService.InsertActivity(
                "EditManufacturer",
                GetLocaleResourceString("ActivityLog.EditManufacturer"),
                manufacturer.Name);

            return manufacturer;
        }

        protected void SaveButton_Click(object sender, EventArgs e) {
            if (Page.IsValid) {
                try {
                    Manufacturer manufacturer = Save();
                    ShowMessage("Manufacturer saved.");
                    //Response.Redirect(string.Format("ManufacturerDetails.aspx?ManufacturerID={0}", manufacturer.ManufacturerId));
                    //Response.Redirect("manufacturers.aspx");
                } catch (Exception exc) {
                    ProcessException(exc);
                }
            }
        }

        //protected void SaveAndStayButton_Click(object sender, EventArgs e) {
        //    if (Page.IsValid) {
        //        try {
        //            Manufacturer manufacturer = Save();
        //            Response.Redirect(string.Format("ManufacturerDetails.aspx?ManufacturerID={0}", manufacturer.ManufacturerId));
        //        } catch (Exception exc) {
        //            ProcessException(exc);
        //        }
        //    }
        //}

        protected void DeleteButton_Click(object sender, EventArgs e) {
            try {
                Manufacturer manufacturer = this.ManufacturerService.GetManufacturerById(this.ManufacturerId);
                if (manufacturer != null) {
                    this.ManufacturerService.MarkManufacturerAsDeleted(manufacturer.ManufacturerId);

                    this.CustomerActivityService.InsertActivity(
                        "DeleteManufacturer",
                        GetLocaleResourceString("ActivityLog.DeleteManufacturer"),
                        manufacturer.Name);
                }
                Response.Redirect("Manufacturers.aspx");
            } catch (Exception exc) {
                ProcessException(exc);
            }
        }

        protected override void OnPreRender(EventArgs e) {
            Manufacturer manufacturer = this.ManufacturerService.GetManufacturerById(this.ManufacturerId);
            if (manufacturer != null) {
                PreviewButton.OnClientClick = string.Format("javascript:OpenWindow('{0}', 800, 600, true); return false;", SEOHelper.GetManufacturerUrl(manufacturer.ManufacturerId));
            }

            base.OnPreRender(e);
        }

        public int ManufacturerId {
            get {
                return CommonHelper.QueryStringInt("ManufacturerId");
            }
        }

        protected string TabId {
            get {
                return CommonHelper.QueryString("TabId");
            }
        }
    }
}